package strategy 
{
	import org.airmvc.Controller;
	import utils.T;
	import vo.CastleVO;
	import vo.GeneralVO;
	
	/**
	 * ...
	 * @author WLDragon 2014-02-28
	 */
	public class StrategyC extends Controller 
	{
		
		public function StrategyC() 
		{
			super();
		}
		
		override protected function addListeners():void 
		{
			interest(Nws.STRATEGY_SWITCH_MAP, handleSwitchMap);
			interest(Nws.STRATEGY_REQUEST_CASTLE, handleRequest);
			interest(Nws.STRATEGY_UPDATE_CASTLE, handleUpdate);
			interest(Nws.STRATEGY_UNACTIVE_CASTLE, function():void { send(UNACTIVE_CASTLE); } );
			interest(Nws.MENU_SHOW_OR_HIDE, function(isShow:Boolean):void { send(SHOW_OR_HIDE,isShow); } );
			
			receive(CLICK_CASTLE,handleSelectCastle);
		}
		
		private function handleUpdate(castleId:int,playerId:int):void 
		{
			send(UPDATE_CASTLE, castleId, playerId);
		}
		
		private function handleRequest(type:int,castles:Array):void 
		{
			curType = type;
			send(CHOOSE_CASTLE, castles);
		}
		
		private function handleSwitchMap():void
		{
			if (isTraffic)
				broadcast(Nws.INFOMATION_SWITCH_CITY, 1);
			else
				broadcast(Nws.INFOMATION_SWITCH_CITY, 0);
				
			isTraffic = !isTraffic;
			send(SWITCH_MAP);
		}
		
		private function handleSelectCastle(id:int):void 
		{
			if (curType == 1)
			{
				active(id);
			}
			else if(curType == 2)
			{
				broadcast(Nws.MENU_VERIFY_PARAM, id);
			}
			else if (curType == 3)
			{
				var a:Array = [];
				var c:CastleVO = T.db.getRecord(Cnt.VO_CASTLE, id);
				for each(var gid:String in c.genList)
				{
					a.push(T.db.getRecord(Cnt.VO_GENERAL,int(gid)));
				}
				broadcast(Nws.INFOMATION_UPDATE_CITY, id);
				broadcast(Nws.GENERAL_OPEN, a, Cnt.MODE_RECONNOITER);
			}
		}
		
		private function active(id:int):void
		{
			if (Gbl.activePlayer.castles.indexOf(id.toString()) > -1)
			{
				broadcast(Nws.STRATEGY_ACTIVE_CASTLE, id);
				broadcast(Nws.INFOMATION_UPDATE_CITY, id);
				send(ACTIVE_CASTLE, true);
				curType = 0;
			}
			else
			{
				broadcast(Nws.MENU_SHOW, 0);
				broadcast(Nws.INFOMATION_SWITCH_CITY, 0);
				var n:String = (T.db.getRecord(Cnt.VO_GENERAL, Gbl.activePlayer.monarchID) as GeneralVO).name;
				Msg.instance.show(n + "大人\n此乃敌方城池", 125, 330);
				send(ACTIVE_CASTLE,false);
			}
		}
		
		
		override protected function startup(... args):void 
		{
			if (view == null)
			{
				//初始化视图
				view  = new StrategyV();
				registerView(view);
				curType = 1;
				send(CHOOSE_CASTLE, Gbl.activePlayer.castles);
			}
			else
			{
				
			}
		}
		
		/**记录地图的状态*/
		private var isTraffic:Boolean;
		/**当前选择城池的状态 1-选择进行活动的城池 2-选择提供参数的城池 3-选择城池侦察*/
		private var curType:int;
		
		/**打开或关闭视图*/
		static public const SHOW_OR_HIDE:String = "show_or_hide";
		/**打开交通图*/
		static public const SWITCH_MAP:String = "switch_map";
		/**点击城池*/
		static public const CLICK_CASTLE:String = "click_castle";
		/**激活城池，闪烁*/
		static public const ACTIVE_CASTLE:String = "active_castle";
		/**选择城池*/
		static public const SELECT_CASTLE:String = "select_castle";
		/**更新城池*/
		static public const UPDATE_CASTLE:String = "update_castle";
		/**取消激活城池*/
		static public const UNACTIVE_CASTLE:String = "unactive_castle";
		/**光标选择城池,[castleId]*/
		static public const CHOOSE_CASTLE:String = "choose_castle";
	}
	
}